<?php

/**
 * WebUser class
 *
 * @author Julot <andy.julot@gmail.com>
 * @since Thursday, June 30, 2011. 06:22 PM
 */

class WebUser extends RWebUser {

	public function getMenu() {
		static $menu = null;

		if (!isset($menu)) {
			$menu = array();
			$user = Yii::app()->user;
			$userModule = Yii::app()->getModule('user');

			$menu[] = array(
				'label'=>'Create Leave',
				'url'=>array('/leave/create'),
				'visible'=>$user->checkAccess('Leave.Create'),
			);
			
			if ($this->isEmployee) {
				if ($this->leaveRequest) {
					$id = $this->leaveRequest->id;
					$menu[] = array(
						'label'=>'Cancel Leave Request',
						'url'=>array('/leave/cancel', 'id'=>$id),
					);
				} else {
					$menu[] = array(
						'label'=>'Request Leave',
						'url'=>array('/leave/request'),
					);
				}

				$statusName = CHtml::activeName(Leave::model(), 'status');
				$label = 'Pending Leave Request Waiting Your Approval ';
				$label .= "[$this->leavePendingCount]";
				$menu[] = array(
					'label'=>$label,
					'url'=>array(
						'/leave/index',
						$statusName=>Leave::STATUS_PENDING
					),
					'visible'=>$this->leavePendingCount,
				);

				$label = 'Supervisor Approved Leave Request Waiting Your ';
				$label .= "Consent [$this->leaveSupervisorApprovedCount]";
				$menu[] = array(
					'label'=>$label,
					'url'=>array(
						'/leave/index',
						$statusName=>Leave::STATUS_APPROVED_SUPERVISOR
					),
					'visible'=>$this->leaveSupervisorApprovedCount,
				);
			}

			$menu[] = array(
				'label'=>'Change Avatar',
				'url'=>'http://www.gravatar.com',
				'linkOptions'=>array('target'=>'_blank'),
			);
			
			$menu[] = array(
				'url'=>$userModule->profileUrl,
				'label'=>'Profile',
			);
		}
		return $menu;
	}

	protected $_employee;

	public function getEmployee() {
		// DO NOT DELETE THIS ONE LINE BELOW!
		// This is to trigger populating _employee variable.
		$this->isEmployee;
		return $this->_employee;
	}

	protected $_isEmployee;

	public function getIsEmployee() {
		if ($this->_isEmployee === null) {
			$condition = 'userId = :userId';
			$params = array(
				':userId'=>Yii::app()->user->id,
			);
			$employee = Employee::model()->find($condition, $params);
			if ($employee) {
				$this->_isEmployee = true;
				$this->_employee = $employee;
			} else {
				$this->_isEmployee = false;
				$this->_employee = null;
			}
		}
		return $this->_isEmployee;
	}

	public function getLeaveRequest() {
		if (!$this->isEmployee || !$this->employee->isSupervisor) {
			return null;
		}

		static $leave = false;

		if ($leave === false) {
			$condition = 'employeeId = :employeeId AND ';
			$condition .= 'status IN (:status1, :status2)';
			$criteria = array(
				'condition'=>$condition,
				'params'=>array(
					':employeeId'=>$this->employee->id,
					':status1'=>Leave::STATUS_PENDING,
					':status2'=>Leave::STATUS_APPROVED_SUPERVISOR,
				),
			);
			$leave = Leave::model()->find($criteria);
		}

		return $leave;
	}

	public function getLeavePendingCount() {
		if (!$this->isEmployee || !$this->employee->isSupervisor) {
			return 0;
		}

		static $count = null;

		if (!isset($count)) {
			$command = Yii::app()->db->createCommand();
			$command->select('count(l.id)');
			$command->from(Leave::model()->tableName() . ' l');
			$command->join(
				Employee::model()->tableName() . ' e', 'l.employeeId = e.id'
			);
			$conditions = array(
				'and',
				'e.supervisorId = :supervisorId',
				'l.status = :status'
			);
			$params = array(
				':supervisorId'=>$this->employee->id,
				':status'=>Leave::STATUS_PENDING
			);
			$command->where($conditions, $params);
			$count = $command->queryScalar();
		}
		return $count;
	}

	public function getLeaveSupervisorApprovedCount() {
		if (!$this->isEmployee || !$this->employee->isManagement) {
			return 0;
		}

		static $count = null;

		if (!isset($count)) {
			$command = Yii::app()->db->createCommand();
			$command->select('count(l.id)');
			$command->from(Leave::model()->tableName() . ' l');
			$condition = 'l.status = :status';
			$params = array(
				':status'=>Leave::STATUS_APPROVED_SUPERVISOR
			);
			$command->where($condition, $params);
			$count = $command->queryScalar();
		}

		return $count;
	}

	public function afterLogin($fromCookie) {
		parent::afterLogin($fromCookie);
		$this->setSetting();
	}

	protected function setSetting() {
		if (!$this->isGuest) {
			$user = User::model()->findByPk($this->id);
			$this->setState('theme', $user->profile->theme);
		}
	}

	protected function restoreFromCookie() {
		parent::restoreFromCookie();
		$this->setSetting();
	}

}
